package sgi.client;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import sgi.details_new.ImageDetails;
import javax.swing.JScrollPane;

public class SGIImageDeleteUI extends SGIClientFrame {

	private JTable table;
	private JPanel contentPane;
	private static SGIImageDeleteController deleteReview;
	/** constructor */
	public SGIImageDeleteUI()
	{
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 656, 525);		
		
		setTitle("Images table");
		contentPane = new JPanel();
		getContentPane().setLayout(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		/** button return to useraccountmenu */
		JButton btnReturn = new JButton("Return");
		btnReturn.setBounds(10, 450, 123, 36);
		getContentPane().add(btnReturn);
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGIImageDeleteController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
		

		

		
		/** creating table */
		Object[] columnNames = {"imageId","DateTime","Location"};
		Object rowData[][]={};
		DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames){
	      public boolean isCellEditable(int rowIndex, int mColIndex) {
	          return false;
	        }
	      };
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 46, 653, 393);
		contentPane.add(scrollPane);
		table = new JTable(dtm);
		table.setBounds(10, 127, 633, 321);
		
		JTableHeader header = table.getTableHeader();
		
		JPanel panel = new JPanel();
		scrollPane.setViewportView(panel);
		panel.setLayout(new BorderLayout());
		panel.add(header, BorderLayout.NORTH);
		panel.add(table, BorderLayout.CENTER);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panel.add(table);
		
		/** button for sending message of selected column for deletion  */
		JButton btnDelete = new JButton("Delete");
		btnDelete.setBounds(520, 450, 123, 36);
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int sel = table.getSelectedRow();
				String imageid=(String) table.getValueAt(sel, 0);
				deleteReview.MessageFromImageDeleteUI(Integer.valueOf(imageid));
				review();
			}
		});
		getContentPane().add(btnDelete);
		

		
		/** setting the table */
		TableColumn column=null;
		for(int i=0;i<table.getColumnCount(); i++)
		{
			column= table.getColumnModel().getColumn(i);
			if(i== 3)
			{

				column.setWidth(200);
			}
			if(i== 4)
			{

				column.setWidth(600);
			}
		}
		

	}

	
	
	/** setting controller in UI  */
	public void setImageDeleteController(SGIImageDeleteController deleteReview)
	{
		SGIImageDeleteUI.deleteReview = deleteReview;
	}
	
	/** adding rows of images to table  */
	public void addRows(ImageDetails cmd)
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.insertRow(table.getRowCount(), new Object[] {String.valueOf(cmd.getImageid()),cmd.getDate(),cmd.getLocation()});
		table.setModel(dtm);
	}
	/** clearing table */
	public void clearTable()
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		while(dtm.getRowCount() > 0) {dtm.removeRow(0);}
	}	
	/** reviewing UI  */
	public void review()
	{
		SGIImageDeleteController.deleteReviewUI.clearTable();
		deleteReview.DeleteReview();	
	}

}
